package com.zhongxi.guoyuan.adapter.shopCart;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.shopcart.AddressListResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.shopCart.EditAddressActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/2/21.
 */

public class AddressListAdapter extends BaseAdapter {

    private Context context;
    private List<AddressListResult.AddressListData> list;
    private Handler handler;
    private int selectPos = -1;

    public AddressListAdapter(Context context, List<AddressListResult.AddressListData> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler = handler;
    }

    public int getSelectPos() {
        return selectPos;
    }

    public void setSelectPos(int selectPos) {
        this.selectPos = selectPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try{
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_address_list,parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
                holder.relaDelete = (RelativeLayout) convertView.findViewById(R.id.relaDelete);
                holder.relaEdit = (RelativeLayout) convertView.findViewById(R.id.relaEdit);
                holder.relaSelect = (RelativeLayout) convertView.findViewById(R.id.relaSelect);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            final AddressListResult.AddressListData data = list.get(position);
            if(null!=data.getUsername()&&!"".equals(data.getUsername())){
                holder.tvName.setText("收货人:"+data.getUsername());
            }else{
                holder.tvName.setText("收货人：");
            }
            if(null!=data.getMphone()&&!"".equals(data.getMphone())){
                holder.tvPhone.setText("电话:"+data.getMphone());
            }else{
                holder.tvPhone.setText("电话:");
            }
            if(null!=data.getAddress()&&!"".equals(data.getAddress())){
                holder.tvAddress.setText("地址:"+data.getProvince()+data.getCity()+data.getPrefecture()+data.getAddress());
            }else{
                holder.tvAddress.setText("地址:");
            }
            holder.relaDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message msg = Message.obtain();
                    msg.what = position;
                    handler.sendMessage(msg);
                }
            });

            holder.relaEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, EditAddressActivity.class);
                    intent.putExtra("data", data);
//                    context.startActivityForResult(intent);
                    ((Activity)context).startActivityForResult(intent, 101);
                }
            });

            if("1".equals(data.getIsdefault())){
                selectPos = position;
                SharedPreferences sp = context.getSharedPreferences(Url.APP_SP, Context.MODE_PRIVATE);
                try{
                    sp.edit().putString("name", data.getUsername()).commit();
                    sp.edit().putString("userphone", data.getMphone()).commit();
                    sp.edit().putString("address", data.getAddress()).commit();
                }catch (Exception e){
                    e.printStackTrace();
                }
                data.setIsdefault("0");
            }

            if(position == selectPos){
                holder.ivImg.setImageResource(R.mipmap.iv_select);
            }else{
                holder.ivImg.setImageResource(R.mipmap.iv_noselect);
            }

            holder.relaSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selectPos!=position){
                        selectPos = position;
                    }else{
                        selectPos = -1;
                    }
                    notifyDataSetChanged();
                }
            });

        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvAddress;
        private ImageView ivImg;
        private RelativeLayout relaDelete;
        private RelativeLayout relaEdit;
        private RelativeLayout relaSelect;
    }
}
